package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.dto.StudentAuthApplyDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.StudentAuthApplyService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author chenmengran
 * @Description: 学生认证
 * @date 2020\1\6 000610:34
 */
@Validated
@RestController
@RequestMapping(value = "/StudentAuth", produces = BaseController.MEDIA_JSON_UTF8)
public class StudentAuthApplyController extends BaseController {
    @Autowired
    private StudentAuthApplyService studentAuthApplyService;
    @Autowired
    private AccountService accountService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String sortName,
                                Integer checkState,
                                String name,
                                String studentNO,
                                String phone,
                                @RequestParam(defaultValue = "1") Integer sign,
                                @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                Integer accountId,
                                String startDate,
                                String endDate,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        /*if (!loginInfo.type.equals(UserConstant.USER_ADMIN) && !loginInfo.type.equals(UserConstant.USER_BRAND) && !loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }*/
        if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else if (loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
            accountId = accountService.getBrandId(loginInfo.id);
        }
        /*else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }*/

        if (sortName != null && !"".equals(sortName)) {
            sortName = sortName + (sign == 1 ? " asc" : " desc");
        }
        //查询总数
        Integer total = studentAuthApplyService.getTotal(accountId, checkState, name, studentNO,phone,startDate,endDate);
        //查询具体用户
//        List<StudentAuthApply> studentAs = studentAuthApplyService.queryPage(accountId, checkState, name, studentNO, sortName, rowCount, pageNO);
        List<StudentAuthApplyDTO> studentAuthApplyDTOS = studentAuthApplyService.queryPageV2(accountId, checkState, name,
                studentNO,phone, sortName, rowCount, pageNO,startDate,endDate);

        return JsonResult.ok(studentAuthApplyDTOS, total);
    }


    /**
     * 导出学生认证信息
     * @return fileName
     */
    @PostMapping("/exportStudentAuth")
    public Result<Object> exportStudentAuth(Integer accountId, String name,
                                            String studentNO,String phone, Integer checkState,
                                            String startDate,
                                            String endDate){
//        List<StudentAuthApply> studentAs = studentAuthApplyService.queryPage(accountId, checkState, name, studentNO, null, 65535, 1);
        List<StudentAuthApplyDTO> studentAuthApplyDTOS = studentAuthApplyService.queryPageV2(accountId, checkState, name,
                studentNO,phone, null, 65535, 1,startDate,endDate);
        ExcelUtil<StudentAuthApplyDTO> excel = new ExcelUtil<>(StudentAuthApplyDTO.class);
        JsonResult result = excel.exportExcel(studentAuthApplyDTOS, "学生认证审核");
        return Result.success(result.getData());
    }


    //修改
    @RequestMapping("/edit")
    public JsonResult edit(String checkOpinion,
                           @NotNull(message = "ver.studentIdNo.notnull") String studentIdNo,
                           @NotNull(message = "ver.applyId.notnull") Integer applyId,
                           @NotNull(message = "ver.checkState.notnull") Integer agree,
                           HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        /*if (!loginInfo.type.equals(UserConstant.USER_ADMIN) && !loginInfo.type.equals(UserConstant.USER_BRAND)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }*/
        if (agree == null) {
            agree = 0;
        }

        studentAuthApplyService.edit(loginInfo, applyId, checkOpinion, agree, studentIdNo);

        return JsonResult.ok();
    }

    @RequestMapping("/clearAuth")
    public JsonResult clearAuth(@NotNull(message = "ver.userId.notNull") Integer userId,
                                HttpServletRequest request){
        /*LoginInfo loginInfo = getLogin(request);
        if (!loginInfo.type.equals(UserConstant.USER_ADMIN) && !loginInfo.type.equals(UserConstant.USER_BRAND)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }*/
        studentAuthApplyService.clearAuthInfo(userId);
        return JsonResult.ok();
    }


}
